<?php
$name = $_POST['name'];
$deviceid = $_POST['device_id'];
include $_SERVER['DOCUMENT_ROOT'].'/phpprojects/conn.php';
$sql = "select * from userinfo where name='$name' and deviceid='$deviceid'";
$rs = mysql_query($sql);
if (mysql_num_rows($rs) == 0) {
    die("invalid user:".$name.$deviceid);
}
$resultArr = mysql_fetch_array($rs);
$id = $resultArr['id'];
$content = $_POST['content'];
$courses_arr = json_decode($content, true);
$sql = "delete from takes where id=$id";
mysql_query($sql);
for ($i = 0; $i < count($courses_arr); $i++) {
	$course = $courses_arr[$i];
	$course_id = $course['course_id'];
	$course_name = $course['course_name'];
	$location = $course['location'];
	$teacher = $course['teacher'];
	$week = $course['week'];
	$day = $course['day'];
	$number = $course['number'];
	$duration = $course['duration'];
	$sql = "insert into course values('$course_id','$course_name','$location','$teacher',0,0,0,'$week',$day,$number,$duration,'')";
	mysql_query($sql);
	$sql = "insert into takes values($id, '$course_id', 'student')";
	mysql_query($sql);
}
echo "success";
